<?php
/**
 * sql解释器
 * User: 16
 * Date: 2018/10/8
 * Time: 17:35
 */
namespace interpreter;

use Exception;

class SqlInterpreter{
    /**
     * 表名
     * @var string
     */
    private $_tablename = "";

    /**
     * 实例
     * @var
     */
    private static $_instance;

    public static function db($tablename){
        if(empty($tablename)){
            throw new Exception("参数错误");
        }

        if(!self::$_instance instanceof self){
            self::$_instance = new self();
        }

        //更新表名
        self::$_instance->setTableName($tablename);

        return self::$_instance;
    }

    /**
     * 插入数据
     * @desc
     * @param $data
     * @author 16
     * @date 2018/10/8
     */
    public function insert($data){
        if (empty($data)) {
            throw new Exception("argument data is null", 400);
        }
        $count = count($data);
        //拼接字段
        $field = array_keys($data);
        $fieldString = '';
        foreach ($field as $k => $v) {
            if ($k === (int)($count - 1)) {
                $fieldString .= "`{$v}`";
                continue;
            }
            $fieldString .= "`{$v}`".',';
        }
        unset($k);
        unset($v);
        //拼接值
        $value = array_values($data);
        $valueString = '';
        foreach ($value as $k => $v) {
            if ($k === (int)($count - 1)) {
                $valueString .= "'{$v}'";
                continue;
            }
            $valueString .= "'{$v}'".',';
        }
        unset($k);
        unset($v);
        $sql = "INSERT INTO `{$this->_tableName}` ({$fieldString}) VALUES ({$valueString})";
        echo $sql . "\n";
    }

    /**
     *  删除数据
     *
     * @param  array $data 数据
     * @return mixed
     */
    public function delete($data=[])
    {
        if (empty($data)) {
            throw new Exception("argument data is null", 400);
        }
        // 拼接where语句
        $count = (int)count($data);
        $where = '';
        $dataCopy = $data;
        $pop = array_pop($dataCopy);
        if ($count === 1) {
            $field = array_keys($data)[0];
            $value = array_values($data)[0];
            $where = "`{$field}` = '{$value}'";
        }else{
            foreach ($data as $k => $v) {
                if ($v === $pop) {
                    $where .= "`{$k}` = '{$v}'";
                    continue;
                }
                $where .= "`{$k}` = '{$v}' AND ";
            }
        }
        $sql = "DELETE FROM `{$this->_tableName}` WHERE {$where}";
        echo $sql . "\n";
    }

    /**
     *  更新一条数据
     *
     * @param  array $data 数据
     * @return mixed
     */
    public function update($data=[])
    {
        if (empty($data)) {
            throw new Exception("argument data is null", 400);
        }
        if (empty($data['id'])) {
            throw new Exception("argument data['id'] is null", 400);
        }
        $set = '';
        $dataCopy = $data;
        $pop = array_pop($dataCopy);
        foreach ($data as $k => $v) {
            if ($v === $pop) {
                $set .= "`{$k}` = '$v'";
                continue;
            }
            $set .= "`{$k}` = '$v',";
        }
        $sql = "UPDATE `{$this->_tableName}` SET {$set}";
        echo $sql . "\n";
    }

    /**
     *  查找一条数据
     *
     * @param  array $data 数据
     * @return mixed
     */
    public function find($data=[])
    {
        if (empty($data)) {
            throw new Exception("argument data is null", 400);
        }
        // 拼接where语句
        $count = (int)count($data);
        $where = '';
        $dataCopy = $data;
        $pop = array_pop($dataCopy);
        if ($count === 1) {
            $field = array_keys($data)[0];
            $value = array_values($data)[0];
            $where = "`{$field}` = '{$value}'";
        }else{
            foreach ($data as $k => $v) {
                if ($v === $pop) {
                    $where .= "`{$k}` = '{$v}'";
                    continue;
                }
                $where .= "`{$k}` = '{$v}' AND ";
            }
        }
        $sql = "SELECT * FROM `{$this->_tableName}` WHERE {$where}";
        echo $sql . "\n";
    }


    /**
     * 修改当前操作表名
     * @desc
     * @author 16
     * @date 2018/10/8
     */
    private function setTableName($tablename){
        $this->_tablename = $tablename;
    }
}